package com.example.springsecurity.service.impl;

import com.example.springsecurity.domain.AssignExperiment;
import com.example.springsecurity.domain.Notice;
import com.example.springsecurity.domain.RoProject;
import com.example.springsecurity.domain.SysUser;
import com.example.springsecurity.mapper.*;
import com.example.springsecurity.service.NoticeService;
import com.example.springsecurity.utils.ResultBean;
import com.example.springsecurity.utils.ifnull.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @ClassName NoticeServiceImpl
 * @Description TODO
 * @Author lvyongwei
 * @Date
 **/
@Service
public class NoticeServiceImpl implements NoticeService {


    @Autowired
    private NoticeMapper noticeMapper;

    @Autowired
    private NoticeUserMapper noticeUserMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private ExperimentMapper experimentMapper;

    @Autowired
    private ProjectTeachMapper projectTeachMapper;

    /**
     * 查看自己发布的所有通知
     * @param notice
     * @return
     */
    @Override
    public List<Notice> getAllNotice(Notice notice) {
        /**
         * 获取当前登录用户发布的所有通知
         */
        return noticeMapper.getAllNotice(notice);
    }


    /**
     * 给用户发布通知
     * @param notice
     * @param userIds
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean issueNoticeUser(Notice notice, List<Long> userIds) {
        /**
         * 发布通知,返回主键id
         */
        noticeMapper.insertNotice(notice);
        Long noticeId = notice.getNoticeId();
        return noticeUserMapper.insertNoticeUser(noticeId,userIds);
    }


    /**
     * 获取所有用户id
     * @return
     */
    @Override
    public List<Long> getAllUserIds() {
        return noticeMapper.getAllUserIds();
    }


    /**
     * 获取用户所有未读通知
     * @param userId
     * @return
     */
    @Override
    public List<Notice> getNoticeNoRead(Long userId){
        return noticeMapper.getNoticeNoRead(userId);
    }


    /**
     * 用户查看发给自己的所有通知信息
     * @param userId
     * @return
     */
    @Override
    public List<Notice> getAllNoticeToMyself(Long userId){
        return noticeMapper.getAllNoticeToMyself(userId);
    }

    /**
     * 根据通知id获取通知详情
     * @param noticeId
     * @return
     */
    @Override
    public ResultBean getNoticeByNoticeId(Long noticeId) {
        try{
            /**
             * 根据通知id获取通知内容
             */
            Notice notice = noticeMapper.getNoticeById(noticeId);
            List<SysUser> sysUsers = null;
            if(!StringUtils.isNull(notice.getExperimentId()) && !StringUtils.isNull(notice.getAssignId())){
                /**
                 * 获取该通知发布给的用户id
                 */
                List<Long> list = noticeMapper.getAllUserIdByNoticeId(noticeId);
                /**
                 * 根据用户id集合获取用户名信息
                 */
                sysUsers = sysUserMapper.getUserByIds(list);
            }
            ResultBean resultBean = new ResultBean();
            resultBean.put("notice",notice);
            resultBean.put("sysUser",sysUsers);
            resultBean.put("code",200);
            resultBean.put("msg","获取成功");
            return resultBean;
        }catch (Exception e){
            ResultBean resultBean = new ResultBean();
            resultBean.put("code",500);
            resultBean.put("msg","获取失败");
            return resultBean;
        }
    }

    /**
     * 根据通知id删除通知
     * @param noticeIds
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteNotice(Long[] noticeIds) {
        noticeMapper.deleteNotice(noticeIds);
        return noticeUserMapper.deleteNoticeUser(noticeIds);
    }


    /**
     * 改变已查看状态
     * @param noticeId  通知id
     * @param userId    用户id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Notice lookNotice(Long noticeId, Long userId) {
        noticeMapper.lookNotice(noticeId,userId);
        return noticeMapper.getNoticeById(noticeId);
    }


    /**
     * 根据教师/负责人id 以及对应的实验项目id 获取到对应的布置信息
     * @param projectId
     * @param userId
     * @return
     */
    @Override
    public List<AssignExperiment> getAssignExperiment(Integer projectId, Long userId) {
        return noticeMapper.getAssignExperiment(projectId,userId);
    }


    /**
     * 根据布置的id获取教师布置给了那些学生  学号
     * @param assignId
     * @return
     */
    @Override
    public List<SysUser> getSysUserByAssignId(Long assignId) {
        List<String> list = noticeMapper.getUserNameByAssignId(assignId);
        return sysUserMapper.getUserByUserNames(list);
    }

    /**
     * 根据用户id获取所有负责的实验
     * @return
     */
    @Override
    public List<RoProject> getProjects(Long userId) {
        /**
         * 首先获取所有负责的实验id
         */
        List<Integer> projectIds = projectTeachMapper.getProjectIdsByUserId(userId);
        /**
         * 根据实验id获取实验信息
         */
        List<RoProject> roProjects = experimentMapper.getProjects(projectIds,null);
        return roProjects;
    }
}
